package com.qq.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DBPool {

    private static String driver = "com.mysql.jdbc.Driver";
    private static String url = "jdbc:mysql://127.0.0.1:3306/library";
    private static String user = "root";
    private static String password = "123456";
    //连接池，做成数组,表示里面有十个元素
    private static Connection[]cons = new Connection[10];
    //计数器
    private static  int count = 0;

    static {
        try {
            Class.forName("driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        try{
            //初始化十个连接
        for (int i = 0; i<cons.length;i++){
            cons[i] = DriverManager.getConnection(url,user,password);
        }
        }catch (SQLException e){
            e.printStackTrace();
        }
    }
    public static Connection getConnection(){
        //决定返回cons里面的哪一个
        count ++;
        //这里的判断是为了防止count超过十，因为只有十个
        if (count>=10) count = 0;
        return cons[count];

    }
}

